package com.fractalist.man.base;

import java.util.Iterator;
import java.util.Map;

import com.fractalist.base.core.utils.MyDateUtils;

public class SqlUtils {

	/**
	 * 生成查询库的sql语句
	 * 
	 * @param tableName
	 * @param conditions
	 * @return
	 */
	public static String genderQuerySql(String tableName, String conditions) {
		return genderQuerySql(tableName, conditions, "*");
	}

	/**
	 * 生成查询库的sql语句
	 * 
	 * @param tableName
	 * @param conditions
	 * @param colums
	 * @return
	 */
	public static String genderQuerySql(String tableName, String conditions,
			String colums) {
		return "select "
				+ ((colums == null || colums.trim().length() == 0) ? "*"
						: colums)
				+ " from "
				+ tableName
				+ ((conditions == null || conditions.trim().length() == 0) ? ""
						: " where " + conditions);
	}

	/**
	 * 生成条件语句
	 * 
	 * @param paramMap
	 * @param type
	 * @return
	 */
	public static String genderConditionsByType(Map<String, Object> paramMap,
			String type) {
		StringBuilder conds = new StringBuilder();
		Iterator<String> it = paramMap.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next();
			Object param = paramMap.get(key);
			conds.append(key
					+ ((key.contains(">") || key.contains("<") || key.trim()
							.endsWith("like")) ? " " : " = ")
					+ (param == null ? null : "'" + param + "'") + " " + type
					+ " ");
		}
		String paramStr = conds.toString();
		if (conds.length() > 0) {
			paramStr = conds.substring(0, conds.length() - (type.length() + 1));
		}
		return paramStr;
	}

	/**
	 * 生成插入库的sql语句
	 * 
	 * @param tableName
	 * @param paramMap
	 * @return
	 */
	public static String genderInsertSql(String tableName,
			Map<String, Object> paramMap) {
		if (tableName == null || tableName.trim().length() == 0
				|| paramMap == null) {
			return "";
		}
		StringBuilder cols = new StringBuilder();
		StringBuilder params = new StringBuilder();
		Iterator<String> it = paramMap.keySet().iterator();
		while (it.hasNext()) {
			String col = it.next();
			Object param = paramMap.get(col);
			cols.append(col + ",");
			params.append((param == null ? null : "'" + param + "'") + ",");
		}
		String colStr = cols.toString();
		String paramStr = params.toString();
		if (colStr.length() > 0) {
			colStr = colStr.substring(0, colStr.length() - 1);
		}
		if (paramStr.length() > 0) {
			paramStr = paramStr.substring(0, paramStr.length() - 1);
		}
		return "insert into " + tableName + "(" + colStr + ") values("
				+ paramStr + ")";
	}

	/**
	 * 生成update的语句
	 * 
	 * @param tableName
	 * @param conditions
	 * @param values
	 * @return
	 */
	public static String genderUpdateSql(String tableName, String conditions,
			String values) {
		return "update "
				+ tableName
				+ " set "
				+ values
				+ ((conditions == null || conditions.trim().length() == 0) ? ""
						: " where " + conditions);
	}

	/**
	 * 打印语句
	 * 
	 * @param sql
	 */
	public static void print(String sql) {
		System.out.println("["
				+ MyDateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss") + "] >> "
				+ sql);
	}

	/**
	 * 打印语句
	 * 
	 * @param sql
	 */
	public static void print(String[] sql) {
		if (sql == null) {
			return;
		}
		for (int i = 0; i < sql.length; i++) {
			print(sql[i]);
		}
	}
}
